package com.controleFamiliar.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailService {
	
	public void enviarEmail(String assunto, String conteudoMensagem){
		// Recipient's email ID needs to be mentioned.
	    String to = "abner.broker@gmail.com";

	    // Sender's email ID needs to be mentioned
	    String from = "abner.broker@gmail.com";

	    // Assuming you are sending email from localhost
	    String host = "smtp.gmail.com";

	    // Get system properties
	    Properties properties = System.getProperties();
	    
	    SmtpAuthenticator authentication = new SmtpAuthenticator();

	    // Setup mail server
	    properties.setProperty("mail.smtp.host", host); 
	    properties.put("mail.smtp.auth", "true");
	    properties.put("mail.debug", "false");  
	    properties.put("mail.smtp.ssl.enable", "true");
	    
	    // Get the default Session object.
	    Session session = Session.getDefaultInstance(properties, authentication);

	    try{
	       // Create a default MimeMessage object.
	       MimeMessage message = new MimeMessage(session);

	       // Set From: header field of the header.
	       message.setFrom(new InternetAddress(from));

	       // Set To: header field of the header.
	       message.addRecipient(Message.RecipientType.TO,
	                                new InternetAddress(to));

	       // Set Subject: header field
	       message.setSubject(assunto);

	       // Now set the actual message
	       message.setText(conteudoMensagem);

	       // Send message
	       Transport.send(message);
	       System.out.println("Sent message successfully....");
	    }catch (MessagingException mex) {
	       mex.printStackTrace();
	    }
	}

}
